package com.neusoft.NursingHome.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.NursingHome.po.Customer;
import com.neusoft.NursingHome.po.ResponseBean;
import com.neusoft.NursingHome.service.CustomerService;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 查询所有的客户
     * @return 包含客户列表的 ResponseBean
     */
    @GetMapping("/all")
    public ResponseBean<List<Customer>> getAllCustomers() {
        List<Customer> customers = customerService.getAllCustomers();
        return new ResponseBean<>(customers);
    }

    /**
     * 通过姓名模糊查询客户
     * @param name 客户姓名
     * @return 包含客户列表的 ResponseBean
     */
    @GetMapping("/searchByName/{name}")
    public ResponseBean<List<Customer>> searchCustomersByName(@PathVariable String name) {
        List<Customer> customers = customerService.searchCustomersByName(name);
        return new ResponseBean<>(customers);
    }

    /**
     * 通过客户类型筛选（护理/自理）
     * @param type 客户类型
     * @return 包含客户列表的 ResponseBean
     */
    @GetMapping("/type/{type}")
    public ResponseBean<List<Customer>> getCustomersByType(@PathVariable String type) {
        List<Customer> customers = customerService.getCustomersByType(type);
        return new ResponseBean<>(customers);
    }

    /**
     * 根据 ID 查询客户详情
     * @param id 客户 ID
     * @return 包含客户详情的 ResponseBean
     */
    @GetMapping("/{id}")
    public ResponseBean<Customer> getCustomerById(@PathVariable Integer id) {
        Customer customer = customerService.getCustomerById(id);
        return new ResponseBean<>(customer);
    }

    /**
     * 获取所有没有健康管家的护理老人
     * @return 包含客户列表的 ResponseBean
     */
    @GetMapping("/withoutManager")
    public ResponseBean<List<Customer>> getCustomersWithoutHealthManager() {
        List<Customer> customers = customerService.getCustomersWithoutHealthManager();
        return new ResponseBean<>(customers);
    }

    /**
     * 分配健康管家
     * @param customerId 客户 ID
     * @param userId 健康管家 ID
     * @return 操作结果的 ResponseBean
     */
    @PostMapping("/assignManager")
    public ResponseBean<Boolean> assignHealthManagerToCustomer(
            @RequestParam Integer customerId,
            @RequestParam Integer userId) {
        boolean result = customerService.assignHealthManagerToCustomer(customerId, userId);
        return new ResponseBean<>(result);
    }

    /**
     * 查询护工服务的所有客户
     * @param userId 护工 ID
     * @return 包含客户列表的 ResponseBean
     */
    @GetMapping("/userId/{userId}")
    public ResponseBean<List<Customer>> getCustomersByUserId(@PathVariable Integer userId) {
        List<Customer> customers = customerService.getCustomersByUserId(userId);
        return new ResponseBean<>(customers);
    }

    /**
     * 将客户的护工置为空
     * @param customerId 客户 ID
     * @return 操作结果的 ResponseBean
     */
    @PostMapping("/removeManager")
    public ResponseBean<Boolean> removeHealthManagerFromCustomer(@RequestParam Integer customerId) {
        boolean result = customerService.removeHealthManagerFromCustomer(customerId);
        return new ResponseBean<>(result);
    }

    /**
     * 逻辑删除客户
     * @param customerId 客户 ID
     * @return 删除结果的 ResponseBean
     */
    @PostMapping("/delete")
    public ResponseBean<Boolean> logicalDeleteCustomer(@RequestParam Integer customerId) {
        boolean result = customerService.logicalDeleteCustomer(customerId);
        return new ResponseBean<>(result);
    }

    /**
     * 添加客户
     * @param customer 客户对象
     * @return 添加结果的 ResponseBean
     */
    @PostMapping("/add")
    public ResponseBean<Boolean> addCustomer(@RequestBody Customer customer) {
        boolean result = customerService.addCustomer(customer);
        return new ResponseBean<>(result);
    }

    /**
     * 更新客户信息
     * @param customer 更新后的客户对象
     * @return 更新结果的 ResponseBean
     */
    @PutMapping("/update")
    public ResponseBean<Boolean> updateCustomer(@RequestBody Customer customer) {
        boolean result = customerService.updateCustomer(customer);
        return new ResponseBean<>(result);
    }
    /**
     * 根据客户 ID 移除其关联的护理级别
     * @param customerId 客户的唯一标识符
     * @return 移除结果的 ResponseBean
     */
    @DeleteMapping("/level/{customerId}")
    public ResponseBean<Boolean> removeCustomerLevel(@PathVariable Integer customerId) {
        boolean result = customerService.removeCustomerLevel(customerId);
        return new ResponseBean<>(result);
    }
    @GetMapping("/searchByNameAndType")
    public ResponseBean<List<Customer>> searchCustomersByNameAndType(
            @RequestParam String name,
            @RequestParam String type) {
        List<Customer> customers = customerService.searchCustomersByNameAndType(name, type);
        return new ResponseBean<>(customers);
    }
    /**
     * 根据客户名称和没有护工（user_id 为空）模糊查询客户
     * @param name 客户姓名
     * @return 包含符合条件的客户列表的 ResponseBean
     */
    @GetMapping("/searchByNameWithoutUserId")
    public ResponseBean<List<Customer>> searchCustomersByNameWithoutUserId(@RequestParam String name) {
        List<Customer> customers = customerService.searchCustomersByNameWithoutUserId(name);
        return new ResponseBean<>(customers);
    }
    
}